package com.shadow.core.net.rx;

import android.content.Context;

import com.shadow.core.net.callback.IError;
import com.shadow.core.net.callback.ISuccess;
import com.shadow.core.net.callback.file.ResponseCallback;
import com.shadow.core.ui.loader.LoaderStyle;

import java.io.File;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by zhangyu on 2017/10/19.
 */

public class RxRestClintBuilder {
    private String mUrl;
    private final WeakHashMap<String, Object> PARAMS = new WeakHashMap<>();
    private RequestBody mBody;
    private LoaderStyle mLoaderStyle;
    private Context mContext;
    private File mFile;
    private ISuccess mSuccess;
    private IError mError;
    private ResponseCallback mFileCallBack;

    RxRestClintBuilder() {

    }

    public final RxRestClintBuilder url(String url) {
        this.mUrl = url;
        return this;
    }

    public final RxRestClintBuilder success(ISuccess success) {
        this.mSuccess = success;
        return this;
    }


    public final RxRestClintBuilder progress(ResponseCallback fileCallBack) {
        this.mFileCallBack = fileCallBack;
        return this;
    }

    public final RxRestClintBuilder error(IError error) {
        this.mError = error;
        return this;
    }

    public final RxRestClintBuilder params(WeakHashMap<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    public final RxRestClintBuilder params(String key, Object value) {
        PARAMS.put(key, value);
        return this;
    }

    public final RxRestClintBuilder raw(String raw) {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }

    public final RxRestClintBuilder loader(Context context, LoaderStyle style) {
        mContext = context;
        mLoaderStyle = style;
        return this;
    }

    public final RxRestClintBuilder loader(Context context) {
        mContext = context;
        mLoaderStyle = LoaderStyle.BallSpinFadeLoaderIndicator;
        return this;
    }

    public final RxRestClintBuilder file(File file) {
        mFile = file;
        return this;
    }

    public final RxRestClintBuilder file(String file) {
        mFile = new File(file);
        return this;
    }


    public final RxRestClient build() {
        return new RxRestClient(mUrl, PARAMS, mBody,
                mLoaderStyle,
                mContext, mFile,
                mFileCallBack, mSuccess, mError);
    }
}
